﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Models
{
    public class ContractCustomertList
    {
        public PageInfo Init()
        {
            PageInfo pg = new PageInfo();
            HttpSessionState session =System.Web.HttpContext.Current.Session;
            pg.dict = BasePage.getDict(session);
            pg.Type = "ContractList";
            pg.LocateText = "查询";
            pg.SMenuId = "KH";
            pg.TableName = "cu_Customers";
            pg.KeyFieldName = "id";
            pg.Sop = BasePage.Grant(session, pg.SMenuId);
            pg.Sop.BROWSE = BasePage.Grant(session, pg.SMenuId, "QY");
            pg.Sop.ADD = pg.Sop.EDIT = pg.Sop.DEL = false;
            pg.Tab.Add("今日到访", "LocatePara='';DataPageUrl='../Main/DataPageLay?type=ContracCustomertList&state=0';GoPage(1);");
            pg.Tab.Add("订金客户", "LocatePara='';DataPageUrl='../Main/DataPageLay?type=ContracCustomertList&state=1';GoPage(1);");
            pg.Tab.Add("签单客户", "LocatePara='';DataPageUrl='../Main/DataPageLay?type=ContracCustomertList&state=2';GoPage(1);");
            pg.Tab.Add("未签客户", "LocatePara='';DataPageUrl='../Main/DataPageLay?type=ContracCustomertList&state=3';GoPage(1);");
            
            HttpRequest request = System.Web.HttpContext.Current.Request;
            String state = Fun.cString(request["state"]);
            String condition = "";

            //按钮
            String button = "";
            switch (state)
            {
                case "0"://今日到访
                    condition = String.Format(" and CustArrive=1 ");                    
                    break;               
                case "1"://订金客户
                    condition = String.Format(" and custState=5 ");                    
                    break;
                case "2"://签单客户
                    condition = String.Format(" and custState=6 ");
                    break;
                case "3"://未签客户
                    condition = String.Format(" and custState=6 and datediff(dd,t7.ConEnd,getdate())<=30 ");//已过期也会显示
                    break;
                default:
                    break;
            }
            button += "<a href=\"javascript:ShowModalUrl('popEdit', '../Customer/Followup?id=-1&Ids=[id]&KeyCodes='+ GetKeyCode('[id]'), '跟进', '', '', '') \"  class='layui-btn layui-btn-xs'>跟进</a>";
            button += "<a href =\"javascript:ShowModalUrl('popEdit', '../Customer/CustomerMore?id=[id]&keyCode='+GetKeyCode([id]), '详情', '', '', '')\" class='layui-btn layui-btn-primary layui-btn-xs'>详情</a><a href=\"javascript:ShowModalUrl('popEdit', '../Customer/CustomerEdit?id=[id]&keyCode='+GetKeyCode([id]), '补充', '', 700, 500)\" class='layui-btn layui-btn-primary layui-btn-xs'>补充</a>";

            pg.Sql = String.Format(@"
select * from (
    select t1.*,
        datediff(m,BirdDate,getdate()) custMonth,		
        t2.dname custStateCn,
		datediff(hh,getdate(),t4.createDate)+ReceInterval as receHour,
		FollowupCount,
		'' link
    from cu_Customers t1
    left join tb_sys_dictionary t2 on t1.CustState = t2.did and t2.typeid = 'D01'
    left join (
	    select cid,min(createDate) contractDate from cu_Contract group by cid
    ) t3 on t1.id=t3.cid
	inner join (
		select ROW_NUMBER() over(partition by cid order by id desc) rn,cid,createDate,ReceInterval from cu_Cus_Distribution
	) t4 on t4.rn=1 and t4.cid=t1.id
	inner join (
		select cid,count(*) FollowupCount from cu_Cus_followup group by cid
	) t5 on t5.cid=t1.id
    left join (
        select ROW_NUMBER() over(partition by cid order by id desc) rn,cid,makeDate,CourseId,CourseState from cu_Cus_followup where IsAuditions=1
    ) t6 on t6.rn=1 and t6.cid=t1.id
    left join cu_Contract t7 on t1.contractId=t7.id
    where 1=1 {0}
) cu_Customers where 1=1", condition);
            ColumnInfo cl;
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custName"; cl.HeadText = "宝宝姓名"; cl.IsCondition = true; cl.ConditionPosition = 1; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "childName"; cl.HeadText = "宝宝小名"; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Decimal; cl.FieldName = "custMonth"; cl.HeadText = "月龄"; cl.IsCondition = true; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "linkName"; cl.HeadText = "家长姓名"; cl.IsCondition = true; cl.ConditionPosition = 1; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custTel"; cl.HeadText = "联系电话"; cl.IsCondition = true; cl.ConditionPosition = 1; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custFromId"; cl.HeadText = "渠道来源"; cl.IsCondition = true; cl.Visible = false; cl.DictTypeID = "D03"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custFrom"; cl.HeadText = "渠道来源"; pg.AddColumn(cl);
            //已跟进=跟进次数，最近分配时间 跟进时间 下次联系时间
            //到访=跟进次数，最近分配时间 跟进时间 预约时间（标色）
            //生日=跟进次数，最近分配时间 跟进时间 出生日期（标色）
            //试听=跟进次数，最近分配时间 跟进时间 试听数量 试听时间 试听课程 入账状态
            //订金=跟进次数，最近分配时间 跟进时间  最近签约时间 已作用/总额	未入账/未使用	作废金额	订金条数
            //
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custState"; cl.HeadText = "状态"; cl.IsCondition = true; cl.Visible = false; cl.DictTypeID = "D01"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "custStateCn"; cl.HeadText = "状态"; pg.AddColumn(cl);
            if (!button.Equals(""))
            { cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "link"; cl.HeadText = "操作"; cl.IsSort = false; cl.Script = button; pg.AddColumn(cl); }

            return pg;
        }
    }
}